package org.ithang.application;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ithang.application.account.service.AccountService;
import org.ithang.tools.model.Action;
import org.ithang.tools.model.ActionResult;
import org.ithang.tools.model.Arith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("app")
public class AboutAction extends Action<Object>{

	@Autowired
	private AccountService accountService;
	
	//每天注册人数
	private String everyDayRegNum="SELECT COUNT(0) num,cday FROM ( "+
    " SELECT DATE_FORMAT(create_time,'%Y-%m-%d') as cday,id FROM customer "+
    " ) a GROUP BY a.cday";
	
	//每日充值金额
	private String ereryRechageAmount="SELECT COUNT(0) num,SUM(amount) amount,cday FROM ( "+
    " SELECT DATE_FORMAT(create_time,'%Y-%m-%d') as cday,amount FROM bc_record "+
    " ) a GROUP BY a.cday";
	
	@RequestMapping("about")
	public ActionResult about(){
		Map<String,Object> data=new HashMap<>();
		
		List<Map<String,Object>> ds=accountService.getDBOperator().getJdbcTemplate().query(everyDayRegNum, new RowMapper<Map<String,Object>>(){

			@Override
			public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				Map<String,Object> row=new HashMap<>(6);
				row.put("num", rs.getInt("num"));
				row.put("day", rs.getString("cday"));
				return row;
			}
			
		});
		data.put("regdata", ds==null?new ArrayList<Map<String,Object>>():ds);
		
		ds=accountService.getDBOperator().getJdbcTemplate().query(ereryRechageAmount, new RowMapper<Map<String,Object>>(){

			@Override
			public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				Map<String,Object> row=new HashMap<>(7);
				row.put("num", rs.getInt("num"));
				row.put("day", rs.getString("cday"));
				row.put("amount", rs.getString("amount"));
				return row;
			}
			
		});
		
		data.put("rechargedata", ds==null?new ArrayList<Map<String,Object>>():ds);
		
		
		return success();
	}
	
	@ResponseBody
	@RequestMapping("reglist")
	public Map<String,Object> reglist(){
		Map<String,Object> data=new HashMap<>();
		List<Map<String,Object>> ds=AccountService.getDBOperator().getJdbcTemplate().query(everyDayRegNum, new RowMapper<Map<String,Object>>(){

			@Override
			public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				Map<String,Object> row=new HashMap<>(6);
				row.put("num", rs.getInt("num"));
				row.put("day", rs.getString("cday"));
				return row;
			}
			
		});
	    if(null!=ds&&!ds.isEmpty()){
	    	int tnum=0;
	    	for(Map<String,Object> d:ds){
	    		tnum+=Integer.parseInt(String.valueOf(d.get("num")));
	    	}
	    	for(Map<String,Object> d:ds){
	    		d.put("tnum", tnum);
	    	}
	    }
		
		data.put("rows", ds==null?new ArrayList<Map<String,Object>>():ds);
		data.put("total", data.size());
		return data;
	}
	
	@ResponseBody
	@RequestMapping("rclist")
	public Map<String,Object> rclist(){
		Map<String,Object> data=new HashMap<>();
		List<Map<String,Object>> ds=AccountService.getDBOperator().getJdbcTemplate().query(ereryRechageAmount, new RowMapper<Map<String,Object>>(){

			@Override
			public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				Map<String,Object> row=new HashMap<>(7);
				row.put("num", rs.getInt("num"));
				row.put("day", rs.getString("cday"));
				row.put("amount", rs.getString("amount"));
				return row;
			}
			
		});
	    if(null!=ds&&!ds.isEmpty()){
	    	double tnum=0;
	    	for(Map<String,Object> d:ds){
	    		tnum=Arith.add(tnum,Double.parseDouble((String.valueOf(d.get("amount")))));
	    	}
	    	for(Map<String,Object> d:ds){
	    		d.put("tamount", tnum);
	    	}
	    }
		
		data.put("rows", ds==null?new ArrayList<Map<String,Object>>():ds);
		data.put("total", data.size());
		return data;
	}
	
}
